package managedbean.SaleQuotation;

import entity.CRM.LineItem;
import entity.CRM.ProductPrice;
import entity.CRM.SaleQuotation;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.DiscountBeanLocal;
import session.stateless.CRM.ProductPriceBeanLocal;
import session.stateless.CRM.SaleQuotationBeanLocal;

@ManagedBean(name="editSaleQuotationManagerBean")
@ViewScoped
public class EditSaleQuotationManagerBean implements Serializable{
    public EditSaleQuotationManagerBean() {}
    @EJB
    private SaleQuotationBeanLocal saleQuotationBeanLocal;
    @EJB
    private ProductPriceBeanLocal productBeanLocal;
    @EJB
    private DiscountBeanLocal discountBeanLocal;
    
    private SaleQuotation saleQuotationToEdit;
    private String statusMessage;
    private Double finalPrice;

    public void initView(PhaseEvent event){
        if(saleQuotationToEdit==null){
                saleQuotationToEdit=(SaleQuotation)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedSaleQuotation");    
                this.setFinalPrice(saleQuotationToEdit.getTotalPrice());
        }
    }
    
    public Double getFinalPrice() {
        return finalPrice;
    }

    public void setFinalPrice(Double finalPrice) {
        this.finalPrice = finalPrice;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }
    
    public SaleQuotation getSaleQuotationToEdit(){
        return saleQuotationToEdit;
    }
    
    public void setSaleQuotationToEdit(SaleQuotation saleQuotation){
        this.saleQuotationToEdit = saleQuotation;
    }
    
    
    
    public void updateSaleQuotation(ActionEvent event){
        try{
            saleQuotationBeanLocal.updateSaleQuotation(saleQuotationToEdit.getId(), saleQuotationToEdit.getLineItems());
            statusMessage = "Sale Quotation Updated";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  "Update Sale Quotation Result: " + statusMessage + " (Sale Quotation ID is " + saleQuotationToEdit.getId().toString() + ")", ""));
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void calculate(ActionEvent event)
    {
        Double temp_Price= 0.0;
        for (LineItem lineItem:saleQuotationToEdit.getLineItems())
        {
            ProductPrice temp_product= productBeanLocal.lookupRange(lineItem.getProductPrice().getProductType(), lineItem.getLineItemQuantity());
            lineItem.setProductPrice(temp_product);
            
            Double temp_discountRate= discountBeanLocal.calculateDiscountRate(lineItem.getProductPrice().getProductType(), lineItem.getLineItemQuantity());
            lineItem.setVolumeDiscountRate(temp_discountRate);
            
            Double temp_finalPrice=temp_product.getProductPrice() * lineItem.getLineItemQuantity() * (1-(saleQuotationToEdit.getCustomDiscount()+temp_discountRate)/100);
            lineItem.setFinalPrice(temp_finalPrice);
            temp_Price+=temp_finalPrice;
        }
        this.setFinalPrice(temp_Price);        
    }
        
}
